import 'dart:math';

import 'package:flutter/material.dart';
import 'package:common/common.dart';

class SimpleSingleSelectionDialog extends StatefulWidget {
  final List items;
  final String title;
  final Function(dynamic selectedItem) onSelect;
  final double width;
  final int currentIndex;

  SimpleSingleSelectionDialog(
      {@required this.items,
      @required this.title,
      @required this.onSelect,
      this.currentIndex = -1,
      this.width}) {
    assert(TUtil.isNotEmptyList(items));
    assert(TUtil.isNotEmptyString(title));
  }

  static show(BuildContext context,
      {@required List items,
      @required String title,
      @required Function(dynamic) onSelect,
      double width,
      int currentIndex}) {
    showDialog(
        context: context,
        child: Dialog(
          child: SimpleSingleSelectionDialog(
            items: items,
            title: title,
            onSelect: onSelect,
            width: width,
            currentIndex: currentIndex,
          ),
        ));
  }

  @override
  State<StatefulWidget> createState() => _SimpleSingleSelectionState();
}

class _SimpleSingleSelectionState extends State<SimpleSingleSelectionDialog> {
  int selected;

  @override
  void initState() {
    super.initState();
    selected = widget.currentIndex;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width ?? 200,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
              padding: EdgeInsets.symmetric(vertical: 20),
              child: Text(
                widget.title,
                textScaleFactor: 1,
                style: TextStyle(fontSize: 16, color: TColor.primeColor),
              )),
          Container(
            height: min(widget.items.length * 40.0,
                MediaQuery.of(context).size.height - 60.0),
            child: ListView.builder(
                itemExtent: 40,
                itemCount: TUtil.lengthOfList(widget.items),
                itemBuilder: (context, index) {
                  return GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      setState(() {
                        selected = index;
                      });
                      widget.onSelect(widget.items[index]);
                      Navigator.of(context).maybePop();
                    },
                    child: Container(
                      alignment: Alignment.center,
                      padding:
                          EdgeInsets.symmetric(vertical: 8, horizontal: 20),
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 1, color: TColor.dividerGray))),
                      child: Text(
                        widget.items[index].toString(),
                        textScaleFactor: 1,
                        style: TextStyle(
                            fontSize: 14,
                            color: selected == index
                                ? TColor.primeColor
                                : TColor.textBlack),
                      ),
                    ),
                  );
                }),
          ),
        ],
      ),
    );
  }
}
